from random import shuffle

deck = ['ha','h2','h3','h4','h5','h6','h7','h8','h9','h10','hj','hq','hk','da','d2','d3','d4','d5','d6','d7','d8','d9','d10','dj','dq','dk','ca','c2','c3','c4','c5','c6','c7','c8','c9','c10','cj','cq','ck','sa','s2','s3','s4','s5','s6','s7','s8','s9','s10','sj','sq','sk']
NPlayers = 2
GameInProgress = True
Winner = False
Pile = []

shuffle(deck) 

def setup_deck(deck,NPlayers):
	CardsToRemove = NPlayers % len(deck)
	CardSeek = len(deck) - CardsToRemove
	del deck[CardSeek:]
	
def InitialStack(playerN,NPlayers,deck):
	declen = len(deck) / NPlayers
	end = playerN * declen
	start = end - declen
	return deck[start:end]
	
def setup_players(deck,NPlayers):
	global Players
	Players ={}
	# heres the cool part!!:::
	for i in range(NPlayers):
		i += 1
		PlayerName = 'Player' + str(i)
		Players.update({PlayerName:InitialStack(i,NPlayers,deck)})
		print 'players list:',Players.keys()
	
def GetPlayerFromNumber(Number):
	return 'Player' + str(Number)
def get_player_hand(player):
	return Players[player]
def add_card_to_hand(player,card):
	Players[player] += [card]

def DealCards():
	ShuffledDeck = shuffle(deck)
	player = 1
	Nplayer = 4

	for card in ShuffledDeck:  
		PlayerName = 'Player' + player
		add_card_to_hand(PlayerName,card)
		player += 1
		if player == Nplayer:
			player = 1
	return 1

def PutCardDown(player):
	global Pile
	global Players
	CurrentPlayerHand = Players[player]
	print 'AccordingToPutCardDown:',CurrentPlayerHand
	Card = CurrentPlayerHand.pop()
	Players[player] = CurrentPlayerHand
	print 'CardToPop:',Card
	Pile.append(Card)

def PutFirstCardDown():
	PutCardDown('Player1')
	
def isJack(card):
	if card == 'hj' or 'dj' or 'cj' or 'sj':
		return '1'
	else:
		return '0'
	
def isQueen(card):
	if card == 'hq' or 'dq' or 'cq' or 'sq':
		return '2'
	else:
		return '0'

def isKing(card):
	if card == 'hk' or 'dk' or 'ck' or 'sk':
		return '3'
	else:
		return '0'

def isAce(card):
	if card == 'ha' or 'da' or 'ca' or 'sa':
		return '4'
	else:
		return '0'

def CheckPile():
	"""pass Pile maybe"""
	if Pile:
	    TopPileCard = Pile[-1]
	else:
	    return 0
	Jack = int(isJack(TopPileCard))
	Queen = int(isQueen(TopPileCard))
	King = int(isKing(TopPileCard))
	Ace = int(isAce(TopPileCard))
	TotalToRemove = Jack + Queen + King + Ace + 1
	print TotalToRemove
	return TotalToRemove

def ReturnPileCardsToRemove(Ncards):
	Npile = len(Pile)
	HAX1 = Ncards
	HAX2 = int(HAX1)
	"""print 'ncards equals' , Ncards"""
	print 'CardsToRemoveFromPile:' , HAX2
	NNumb = Npile - HAX2
	if HAX2 < Npile:
		return Pile
	else:
		return Pile[NNumb:Npile]

def ReturnNPileCardsToRemove(Ncards):
	Npile = len(Pile)
	NNumb = Npile - Ncards
	if Ncards < Npile:
		return NNumb
	else:
		return Npile

def PickCardsUp(player,Ncards):
	CardsToAdd = ReturnPileCardsToRemove(Ncards)
	for card in CardsToAdd:
		add_card_to_hand(player,card)
	
	

def RemoveCardsFromPile(Ncards):
	global Pile
	"""sort the DANM GLOBALS OUT"""
	if Pile:
		Npile = len(Pile)
	else:
		Npile = 0
	if len(Pile)< Npile:
		del Pile
	else:
		del Pile[Npile-Ncards:Npile]

def CheckWin():
	PlayersWithCards = 0
	for player in Players.keys():
		if Players[player] != 0:
			PlayersWithCards += 1
	if PlayersWithCards == 1:
		return True
	else:
		return False
	
"""def CheatSkipPlayer(PlayerNumber):
	PlayerNumber += 1
	return GetPlayerFromNumber(PlayerNumber)
def CheatWinGame(PlayerNumber,Passphrase):
	if Passphrase == "superuser":
		return True
	else:
		return False
"""
def TakeTurn(player):
	Ncards = ReturnNPileCardsToRemove(CheckPile())
	PutCardDown(player)
	PickCardsUp(player,Ncards)
	RemoveCardsFromPile(ReturnNPileCardsToRemove(CheckPile()))

	
def PlayBegger(verbose,cheating):
	TurnNumber = 0
	NPlayersR = range(1,NPlayers)
	GameInProgress = True
	while GameInProgress == True:
		for player in Players.keys():
			TurnNumber += 1
			print 'Turn Number' , str(TurnNumber)
			TakeTurn(player)
			
			print '\n' , 'Player' , player , ' hand:' , '\n'
			print get_player_hand(player)
			print 'current pile:',Pile
			if CheckWin() == True:
				print "\n"
				print 'Player ' , player , ' has won!'
				Winner = player
				GameInProgress = False
"""			if CheatWinGame(player,Passphrase):
				print "\n"
				print 'Player ' , player , ' has won!'
				Winner = 'Player' , player
				GameInProgress = False
"""				
setup_deck(deck,2)
setup_players(deck,2)
PutFirstCardDown()
PlayBegger(1,0)
